package com.invite.me.ui.home.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.invite.me.R;
import com.invite.me.base.BaseFragment;
import com.invite.me.beans.adpater.BlindDataAdpater;
import com.invite.me.beans.bilnddatabean.BlindDataBean;
import com.invite.me.beans.fabean.ShimingRzCBean;
import com.invite.me.beans.presenter.BlindDataPresenter;
import com.invite.me.beans.res.BlindDataFragmentBean;
import com.invite.me.ui.cehualan.NameAttestationActivity;
import com.invite.me.ui.yaoyue.BlindDataParticularsActivity;
import com.invite.me.util.SspPrefUtils;
import com.invite.me.util.ToastUtils;
import com.invite.me.wlqq.Constants;
import com.invite.me.wlqq.Contract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BlindDataFragment extends BaseFragment implements View.OnClickListener, Contract.BaseView{

    private SmartRefreshLayout myxiangqinsmartrefresh;
    private RecyclerView xiangqin_recycler;
    BlindDataAdpater baseQuickAdapter;
    List<BlindDataFragmentBean.DataBean> list=new ArrayList<>();
    BlindDataPresenter blindDataPresenter;
    private String state;
    private static List<BlindDataBean> listNew;
    //地图
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;


    @Override
    protected void initView(View view) {
        blindDataPresenter=new BlindDataPresenter(this);

        xiangqin_recycler = view.findViewById(R.id.xiangqin_recycler);
        myxiangqinsmartrefresh = view.findViewById(R.id.myxiangqinsmartrefresh);
        myxiangqinsmartrefresh.setEnableRefresh(false);//是否启用下拉刷新功能
        myxiangqinsmartrefresh.setEnableLoadMore(true);//是否启用上拉加载功能
        //    shuju();
        GridLayoutManager layoutManage = new GridLayoutManager(getActivity(), 2);
        xiangqin_recycler.setLayoutManager(layoutManage);
        baseQuickAdapter=new BlindDataAdpater(R.layout.blind_data_list,list);
        xiangqin_recycler.setAdapter(baseQuickAdapter);

        baseQuickAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.yaoyue:

                        if(!TextUtils.isEmpty(state)){
                            if(state.equals("0")){
                                Intent smrz = new Intent(getActivity(), NameAttestationActivity.class);
                                startActivity(smrz);
                            }else if(state.equals("1")){
                                ToastUtils.show(getActivity(),"实名认证审核中，请耐心等待。");

                            }else if(state.equals("2")){
                                String id = list.get(position).getId();
                                Intent yaoy=new Intent(getActivity(),BlindDataParticularsActivity.class);

                                yaoy.putExtra("id",id);

                                startActivity(yaoy);
                            }else if(state.equals("3")){
                                ToastUtils.show(getActivity(),"审核拒绝请按正确的信息提交");

                            }else if(state.equals("4")){
                                ToastUtils.show(getActivity(),"你的账号已冻结");
                            }
                        }else {
                            Intent smrz = new Intent(getActivity(), NameAttestationActivity.class);
                            startActivity(smrz);
                        }




                        break;

                }

            }
        });



    }
    /***
     * 初始化经纬度
     */
    private void initLocation() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getActivity());
        //设置定位回调监听
        mLocationClient.setLocationListener(mAMapLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //获取一次定位结果：
        mLocationOption.setOnceLocation(true);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }
    //异步获取定位结果
    AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {

                if (amapLocation.getErrorCode() == 0) {
                    Log.e("AMapLocation","进来" );
                    int locationType = amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                    double latitude = amapLocation.getLatitude();//获取纬度
                    double longitude = amapLocation.getLongitude();//获取经度
                    float accuracy = amapLocation.getAccuracy();//获取精度信息
                    String address = amapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
                    String country = amapLocation.getCountry();//国家信息
                    String province = amapLocation.getProvince();//省信息
                    String cityy = amapLocation.getCity();//城市信息

                    String district = amapLocation.getDistrict();//城区信息
                    String street = amapLocation.getStreet();//街道信息
                    String streetNum = amapLocation.getStreetNum();//街道门牌号信息
                    String cityCode = amapLocation.getCityCode();//城市编码
                    String adCode = amapLocation.getAdCode();//地区编码
                    String aoiName = amapLocation.getAoiName();//获取当前定位点的AOI信息
                    String buildingId = amapLocation.getBuildingId();//获取当前室内定位的建筑物Id
                    String floor = amapLocation.getFloor();//获取当前室内定位的楼层
                    int gpsAccuracyStatus = amapLocation.getGpsAccuracyStatus();//获取GPS的当前状态

                    //获取定位时间
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = new Date(amapLocation.getTime());
                    String format = df.format(date);
                    Log.e("AMapLocation","" + locationType+"-"+latitude+"-"+longitude+"-"+accuracy+"-"+address+"-"+country+"-"+province+"-"+"-"+district+"-"+street+"-"+streetNum+"-"+cityCode+"-"+adCode+"-"+aoiName+"-"+buildingId+"-"+floor+"-"+gpsAccuracyStatus);

                    blindDataPresenter.GengXingerenWz(latitude,longitude,"");

                    mLocationClient.stopLocation();//停止定位后，本地定位服务并不会被销毁
                }else {
                    Log.e("AmapError","location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();

        if(list!=null&&list.size()>0){
            list.clear();
        }
        //查询实名认证
        blindDataPresenter.shilie();
        initLocation();

    }
    public void  shuju(){
//    for (int i=0;i<1;i++){
//        list.add(new BlindDataBean("小美矿囧了反萨开机放量阿空降幅凯撒"+i,"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","200"));
//        list.add(new BlindDataBean("小美矿囧了反萨开机放量阿空降幅凯撒"+i,"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","100"));
//        list.add(new BlindDataBean("小美矿囧了反萨开机放量阿空降幅凯撒"+i,"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","100"));
//        list.add(new BlindDataBean("小美矿囧了反萨开机放量阿空降幅凯撒"+i,"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","300"));
//
//    }






    }
    // 遍历后判断赋给另一个List集合，保持原来顺序
    public static void ridRepeat1(List<String> list) {
        System.out.println("list = [" + list + "]");
        List<String> listNew = new ArrayList<String>();
        for (String str : list) {
            if (!listNew.contains(str)) {
                listNew.add(str);
            }
        }
        System.out.println("listNew = [" + listNew + "]");
    }

    @Override
    protected void loadData() {





    }
    private void data(){


    }

    @Override
    protected int getFrgamentLayoutId() {
        return R.layout.fragment_blinddata;
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setPresenter(Contract.BasePresenter basePresenter) {
        if (blindDataPresenter instanceof Contract.BasePresenter) {
            this.blindDataPresenter = (BlindDataPresenter) basePresenter;
        }
    }

    @Override
    public void onDataSucess(Object o) {
        if(o instanceof ShimingRzCBean){
            ShimingRzCBean shimingRzCBean= (ShimingRzCBean) o;

            if(shimingRzCBean.getCode()==1000){
                if(!TextUtils.isEmpty(shimingRzCBean.getData().getState())&&!shimingRzCBean.getData().getState().equals("")){
                    state =shimingRzCBean.getData().getState();
                }



            }


        }else if(o instanceof BlindDataFragmentBean){
            BlindDataFragmentBean blindDataBean= (BlindDataFragmentBean) o;
            if(blindDataBean.getData()!=null){
                list.addAll(blindDataBean.getData());
                baseQuickAdapter.notifyDataSetChanged();
            }

        }

    }

    @Override
    public void onDataFaile(Object o) {

    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLocationClient != null) {
            //销毁定位客户端之后，若要重新开启定位请重新New一个AMapLocationClient对象。
            mLocationClient.onDestroy();//销毁定位客户端，同时销毁本地定位服务。
        }
    }
}